package com.lvfeng.controller;

import com.alibaba.fastjson.JSONObject;
import com.lvfeng.tool.NetTool;
import org.springframework.web.bind.annotation.*;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

@RestController("/")
public class ReqDataController {
    /**
    * 001.接口示例
    * 参考：https://blog.csdn.net/suki_rong/article/details/80445880
    * 跨域：https://blog.csdn.net/tg928600774/article/details/80325040
    * localhost:8088/data/1?name=zs&age=20
    */
    @ResponseBody
    @CrossOrigin    //接收跨域请求
    @GetMapping("/data/{id}")
    public Map getReqData(@PathVariable(name = "id") Long id, @RequestParam(name = "name") String name, @RequestParam(name = "age") int age){
        System.out.println(id + name + age);
        Map map = new HashMap();
        map.put("id",id);
        map.put("name",name);
        map.put("age",age);
        return map;
    }

    /**
     * 002.跨域对话机器人请求
     * 青云客：http://api.qingyunke.com/
     * 接口：http://api.qingyunke.com/api.php?key=free&appid=0&msg=关键词
     */
    @ResponseBody
    @CrossOrigin
    @GetMapping("/talk/{id}")
    public Map getTalk(@PathVariable(name = "id") Long id, @RequestParam(name = "msg") String msg) throws Exception{
        System.out.println("请求内容：" + msg);
        String url = "http://api.qingyunke.com/api.php";
        Map param = new HashMap();  // 配置
        param.put("key","free");
        param.put("appid","0");
        param.put("msg",msg);
        String res = NetTool.getNetData(url, param, "GET");     // 执行请求
        JSONObject object = JSONObject.parseObject(res);
        Map map = object.getInnerMap();    // 结果集转map
        return map;
    }



}
